home *** CD-ROM | disk | FTP | other *** search
/ PC-Blue - MS DOS Public Domain Library / PC-Blue MS-DOS Public Domain Library - NYACC.iso / vol174 / crcb12.asm < prev    next >
Encoding:
Assembly Source File  |  1986-11-24  |  22.4 KB  |  959 lines

  1. TITLE    'MSDOS CrcBuild.asm Version 1.2'
  2.  
  3. ;MSDOS version by    Howard Vigorita
  4. ;            NYACC, CP/M SIG
  5. ;    upload comments &/or updates to (718) 539 - 3338
  6. ;
  7. ;    (derived from CP/M-86 version by Bill Bolton)
  8. ;
  9. ;CRCBUILD is a program to build a file in SIG/M 
  10. ;-CATALOG format and print a Cyclic-Redundancy-Check
  11. ;number based on the CCITT standard polynominal:
  12. ;
  13. ;   X^16 + X^15 + X^13 + X^7 + X^4 + X^2 + X + 1
  14. ;
  15. ;Special Note:    this version of CRCBUILD corrects a file read bug common 
  16. ;        to a number of MSDOS CRCK implementations and will generate 
  17. ;        different CRC values for files not an exact multiple of 128 
  18. ;        bytes in size. Do not use this version with a CRCK program
  19. ;        which generates '00 00' CRC values for small files of
  20. ;        less than 128 bytes. This version is compatable with
  21. ;        CRCK version 6.3 or higher which also fixes that bug.
  22. ;
  23. ;Original CP/M-80 Version by JOHN L. RAFF WB2MDG/KAPP8107
  24. ;
  25. ;VERSION LIST, most recent version first
  26. ;
  27. ;11/Nov/85 Added new routine 'bin_2_dec' which supports binary to
  28. ;          ascii-decimal conversions up to 65535 decimal. Also modified
  29. ;          the block-to-K translation routine (in 'finish') to support
  30. ;          file sizes up to 8191K. These new routines are used to
  31. ;          accurately report file sizes up to 999K w/o alteration of
  32. ;          the -CATALOG output format. Line number output routines also
  33. ;          modified to use the new routine but 99 file limit unchanged.
  34. ;                MSDOS, Version 1.2, Howard Vigorita
  35. ;
  36. ;26/Oct/85 Supress headers & footers for general purpose use.
  37. ;                MSDOS, Version 1.1a, Howard Vigorita
  38. ;
  39. ;25/Oct/85 Corrected bug in disk read routine (peculiar to MSDOS)
  40. ;          whereby the last read block is ignored for files
  41. ;          not an exact multiple of 128 bytes in length. Use with
  42. ;          CRCK version 6.3 or higher for compatability.
  43. ;                MSDOS, Version 1.1, Howard Vigorita
  44. ;
  45. ;10/Oct/85 Converted to MSDOS MASM for PC-Blue use. Use only with
  46. ;          pre-existing versions of CRCK which do not process partial
  47. ;          sectors (e.i., small files of less than 128 bytes generate
  48. ;          a CRC of '00 00').
  49. ;                MSDOS, Version 1.0, Howard Vigorita
  50. ;
  51. ;30/Oct/84 Added CLOSER routine from CRC-64.A86 for use with
  52. ;       Concurrent PCDOS. Version 1.1 Bill Bolton
  53. ;
  54. ;04/Aug/84 CP/M-86 version created from CP/M-80 version. Removed
  55. ;       all in line prints and restructured for 8086 small
  56. ;       memory model. Removed a number of superfluous labels
  57. ;       and some unused data storage. Now uses operating
  58. ;       system 96 byte stack and tested for catalogs
  59. ;       upto 99 entries. If there are more than 100 entries
  60. ;       in a catalog, ID numbers after 99 will display
  61. ;       oddly (the CP/M-80 version does this too).
  62. ;       Version 1.0, Bill Bolton, Software Tools, Australia
  63. ;
  64. ;    *********** NOTE WELL ************
  65. ;
  66. ;    This version uses the "old" CRC polynomial code as
  67. ;    described in EDN all those years back and generates
  68. ;    the same CRCKs as most other CRC programs, including
  69. ;    the CRC options in YAM, MODEM and MEX. Note that these
  70. ;    implementations differ from some communications versions
  71. ;    which append two null bytes to the data stream from
  72. ;    which the CRC is derived. Those implementations only do
  73. ;    that so that the receiver can process the data received
  74. ;    together with the transmitted CRC and generate a result
  75. ;    of '00 00'. Such practice, although technically sound,
  76. ;    produces entirely different CRC values than those found in 
  77. ;    the CRCKLIST, CRCKFILE and -CATALOG files on most public
  78. ;    domain disks.
  79.  
  80.     ;    conditional assembly switches
  81.     ;    -----------------------------
  82. true        equ    0FFh
  83. false        equ    0
  84.  
  85. gen_header    equ    true        ; generate -CATALOG header?
  86. gen_footer    equ    true        ; generate -CATALOG footer?
  87. crc_dash    equ    false        ; CRC filenames starting with '-'?
  88.  
  89.     ;  
  90.     ;dos EQUATES
  91.     ;
  92. rdcon    equ    1
  93. wrcon    equ    2
  94. print    equ    9
  95. rcbuf    equ    10            ;read console buffer
  96. cstat    equ    11
  97. open    equ    15
  98. close    equ    16
  99. srchf    equ    17
  100. srchn    equ    18
  101. delete    equ    19
  102. read    equ    20
  103. write    equ    21
  104. make    equ    22
  105. rename    equ    23
  106. stdma    equ    26
  107. dos    equ    33            ;MSDOS entry point
  108. setiv    equ    25h            ;    set interrupt vector
  109. xctl_c    equ    33h            ;     extend ctl-c checking
  110. CrtStrm    equ    3Ch            ;    creat file stream
  111. CloStrm    equ    3Eh            ;    close file stream
  112. WrtStrm    equ    40h            ;    write to a stream
  113. TrmProc    equ    4Ch            ;    terminate process
  114.  
  115. strlmt    equ    80            ;string LENGTH LIMIT
  116.  
  117. tab    equ    09H            ;ASCII tab
  118. lf    equ    0AH            ;ASCII line feed
  119. cr    equ    0DH            ;ASCII carriage return
  120. eof    equ    1AH            ;end of file marker
  121.  
  122. sect    equ    80h            ;Size of dos sector
  123.  
  124.  
  125. ; Base Page Program Segment Prefix
  126. ; --------------------------------
  127. ;
  128. basepg    SEGMENT AT 0
  129.     ;        base page fields of interest
  130.     ORG    5Ch
  131. bfcb    db    12 dup(?)
  132. bfcbext    db    4 dup(?)
  133. bfcb2    db    16 dup(?)
  134. bfcbrno    db    (?)
  135.     ;
  136.     ORG    80h
  137. bdta    db    128 dup(?)
  138.     ;
  139. basepg    ENDS
  140.  
  141.  
  142. ; Program Data Storage Area
  143. ; -------------------------
  144. ;
  145. data    SEGMENT PARA MEMORY
  146.  
  147. tcatz    db    '-CATALOG.$$$',0
  148. tcath    dw    0
  149.  
  150. signon    db    'CrcBuild, MSDOS Ver 1.2, 12/Nov/85, Howard Vigorita',cr,lf
  151.     db    'CTL-S pauses, CTL-C aborts',cr,lf,cr,lf,'$'
  152.  
  153. wrong    db    'Only decimal digits, please',cr,lf,'$'
  154.  
  155. enter    db    'Enter -CATALOG.xxx number (three digits) - $'
  156.  
  157.     if gen_header
  158. header    db    'PC/Blue         Volume '
  159. header1 db    'NNN',cr,lf
  160.     db      'index   name          size   crc     description'
  161.     db    cr,lf,'$'
  162.     endif
  163.  
  164.     if gen_footer
  165. foot1    db    cr,lf,cr,lf
  166.     db    'PC/Blue Library -CATALOG Volume Number-'
  167. DoneExt    db    'NNN, $'
  168.  
  169. foot2    db    '   files cataloged',cr,lf
  170.     db    '                 compiled and distributed by the',cr,lf
  171.     db      '               New York Amateur Computer Club, Inc',cr,lf
  172.     db    '         Box 106, Church St Station, New York, NY  10008'
  173.     db    cr,lf,'$'
  174.     endif
  175.  
  176. DoneMsg    db    cr,lf,'done$'
  177.  
  178. number    db    cr,lf
  179. fname1    db    'NNN.','$'
  180.  
  181. filen    db    'xx  '
  182. fname    db    'xxxxxxxx.xxx  ','$'
  183.  
  184. fail    db    ' ++ Open Failed ++','$'
  185.  
  186. ksize    db    'xxxK   ','$'
  187.  
  188. ferror    db    tab,tab,'++ File Read Error ++',cr,lf,'$'
  189.  
  190. Nfound    db    '++ File not Found ++$'
  191.  
  192. abort    db    cr,lf,cr,lf,'++ ABORTED ++$'
  193.  
  194. FullMsg    db    cr,lf
  195.     db    'Disk Full: CATALOG'
  196.     db    '$'
  197. dtaMSG    db    cr,lf
  198.     db    'Disk transfer area too small for disk write: CATALOG'
  199.     db    '$'
  200.  
  201. space    db    cr,lf
  202.     db    'No directory space: CATALOG'
  203.     db    '$'
  204.  
  205. ClsMsg    db    cr,lf
  206.     db    'Cannot close CATALOG'
  207.     db    '$'
  208.  
  209. fcbnew    db    0,'-CATALOGNNN'
  210.     db    0
  211.     db    24 dup(?)
  212.  
  213. Wflag    db    0            ;file write flag
  214. remain    dw    0            ;CRC accumulator
  215. quanek    dw    0            ;count of 128 byte blocks
  216. mess    db    0            ;CRC message character
  217. MFflg    db    1            ;1st time switch
  218. LineNum    db    0            ;quantity of files read    
  219.  
  220. fcbCat    db    0,'-CATALOG$$$',0    ;continued in dynamic storage area
  221.  
  222. dsa    equ    $            ; start of dynamic storage area
  223.  
  224.  
  225. fcbcat_cont    equ    dsa        ;db    24 dup(?)
  226.  
  227.     ;fcb components - 37 bytes
  228.     ;
  229. fcb    equ    fcbcat_cont+24    ;db 12 dup(?) drive, fname, & ftype
  230. fcbext    equ    fcb+12        ;db 4 dup(?) current block & rec size
  231. fcb2    equ    fcbext+4    ;db 16 dup(?) file size, date, & time
  232. fcbrno    equ    fcb2+16        ;db 5 dup (?) current rec & rel rec
  233.  
  234.  
  235. mfreq    equ    fcbrno+5    ;db 12 dup(?) requested name
  236. mfcur    equ    mfreq+12    ;db 12 dup(?) current name
  237. bufad    equ    mfcur+12    ;db 2 dup(?) read buffer address
  238. strcnt    equ    bufad+2        ;db 2 dup(?) string count storage
  239. string    equ    strcnt+2    ;db 11 dup(?) string storage
  240. str1    equ    string+11    ;db 12 dup(?) name storage
  241. str2    equ    str1+12        ;db 8 dup(?) characters after name
  242. str3    equ    str2+8        ;db 5 dup(?) ASCII CRC characters
  243. str4    equ    str3+5        ;db 5 dup(?) buffer
  244.  
  245.     ;
  246.     ;write buffer location
  247.     ;
  248. catbuf    equ    str4+5        ; db (?)    
  249.  
  250. dta    equ    catbuf+1    ; db (?) disk transfer area
  251.  
  252.  
  253. data    ENDS
  254.  
  255. ;
  256. ; PROGRAM STARTS HERE
  257. ; -------------------
  258. ;
  259. code    SEGMENT para public
  260.     assume    CS:code, DS:basepg, SS:a_stack
  261.  
  262.     ; note that data segment register is left pointing at the base page
  263.     ; program segment prefix pending a copy to our own prog data area
  264.     ;
  265. crcbuild:
  266.  
  267.     mov    AX,data        ;init extra segment register
  268.     mov    ES,AX            ; to point to prog data area
  269.     assume    ES:data
  270.  
  271.     ; copy base page file control block to our own fbc in prog data area
  272.     ;
  273.     mov    SI,offset bfcb        ;base pg fcb to source index
  274.     mov    DI,offset fcb        ;our fcb to dest index
  275.     mov    CX,37            ;# of bytes to move
  276.     rep movsb            ;block move em    
  277.  
  278.     ; copy base page disk transfer buffer to our own dta in prog data area
  279.     ;
  280.     mov    SI,offset bdta        ;base pg dta to source index
  281.     mov    DI,offset dta        ;our dta to dest index
  282.     mov    CX,64            ;# of words to move
  283.     rep movsw            ;block move em    
  284.  
  285.     ; point the data segment register at our own data area
  286.     ;
  287.     push    ES
  288.     pop    DS
  289.     assume    DS:data 
  290.  
  291.     ; intercept the ctl-C interrupt vector
  292.     ;
  293.     mov    AX,setiv*256+23h
  294.     mov    DX, offset abext    ; point it to our routine
  295.     push    DS            ; save program data segment
  296.     push    CS            ; put code segment into
  297.     pop    DS            ;  DS for interrupt handler
  298.     int    dos
  299.     pop    DS            ; restore our data segment
  300.  
  301.     ; turn on extended ctl-c detection
  302.     ;
  303.     mov    AX,xctl_c*256+1
  304.     mov    DL,1
  305.     int    dos
  306.     
  307. begin:
  308.     xor    AL,AL
  309.     mov    byte ptr LineNum,AL    ;zero the line number counter
  310. bgn2:
  311.     call    CrLf            ;turn up a new line
  312.     mov    BX,offset signon    ;Tell them who we are
  313.     call    display
  314.     mov    AL,fcb+1        ;get 1st fname char in base page fcb
  315.     cmp    AL,' '            ;SEE IF NAME THERE
  316.     jnz    begin2            ;YES, CONTINUE
  317.     mov    DI,offset fcb+2        ;PUT TOTAL DIRECTORY PATTERN
  318.     mov    SI,offset fcb+1        ;IN fcb
  319.     mov    byte ptr [SI],'?'    ;ALL "?"
  320.     mov    CX,10            ;4 CHARACTERS
  321.     rep    movsb
  322.     jmp    short begin2        ;GO GET UM
  323.  
  324. ThrNum:                    ;CHECK FOR THREE ASCII NUMBERS
  325.     call    TwoNum
  326.     jz    Thr1
  327.     ret                ;RETURN IF NOT NUMBERS
  328.  
  329. Thr1:
  330.     call    num
  331.     jnz    Thr2
  332.     ret                ;RETURN IF A number
  333.  
  334. Thr2:
  335.     cmp    AL,'.'            ;CHECK FOR A PERIOD
  336.     jz    Thr3
  337.     ret                ;RETURN IF NOT A PERIOD
  338.  
  339. Thr3:
  340.     dec    BX            ;OTHERWISE BACK UP ONE CHARACTER
  341.     dec    CH            ;AND ONE COUNT
  342.     cmp    AL,AL            ;SET ZERO FLAG
  343.     ret
  344.  
  345. TwoNum:
  346.     call    num
  347.     jz    num
  348.     ret
  349.  
  350. num:
  351.     mov    AL,byte ptr [BX]    ;GET THE CHARACTER
  352.     inc    BX            ;FOR NEXT CHARACTER
  353.     inc    CH            ;INCREMENT FOR NEXT COUNT
  354.     cmp    AL,'0'            ;IS IT LESS THAN 0
  355.     jb    NumNot            ;BRANCH IF IT IS
  356.     cmp    AL,'9'+1        ;IS IT GREATER THAN 9?
  357.     jnb    NumNot
  358.     cmp    AL,AL            ;SET ZERO FLAG
  359.     ret
  360.  
  361. NumNot:
  362.     cmp    AL,'0'            ;SHOULD NOT BE EQUAL, RESET ZERO FLAG
  363.     ret
  364.  
  365. Beg2NG:
  366.     mov    BX,offset wrong
  367.     call    display
  368. begin2:
  369.     mov    BX,offset enter
  370.     call    display
  371.     mov    DX, offset str4        ;point to input buffer
  372.     mov    byte ptr str4,4        ;write max chars wanted in 1st byte
  373.     mov    AH,rcbuf        ;read console buffer
  374.     int    dos
  375.     call    crlf
  376.     cmp    byte ptr str4+1,3    ;see if 3 characters returned
  377.     jnz    Beg2NG            ;try again if not
  378.     mov    BX,offset str4+1    ;point to count of characters entered
  379.     xor    CH,CH            ;zero the counter
  380.     inc    BX
  381. Beg2a:
  382.     call    num            ;IS IT A DECIMAL number?
  383.     jnz    Beg2NG            ;BRANCH IF NOT A DECIMAL number
  384.     mov    AL,CH            ;CHECK QUANTITY OF CHARACTERS
  385.     cmp    AL,3
  386.     jnz    Beg2a            ;BRANCH IF NOT END OF CHARACTERS YET
  387.     mov    byte ptr [BX],0        ;ENSURE TERMINATION OF string
  388.     mov    SI,offset str4+2    ;COPY number INTO FILE string
  389.     mov    DI,offset fname1
  390.     mov    CX,3            ;COUNT
  391.     rep    movsb
  392.     call    CrLf            ;turn up a new line
  393.     mov    AL,'F'            ;INDICATE write FILE open
  394.     mov    byte ptr Wflag,AL
  395.  
  396.     ;'DECLARE' fcb FOR OUTPUT FILE
  397.     ;(TEMPORARILY NAMED -CATALOG.$$$)
  398.     ;
  399.     jmp    MakeCat
  400.  
  401. PutCatalog:
  402.     mov    byte ptr catbuf,AL
  403.     mov    DX, offset catbuf
  404.     mov    CX,1
  405.     mov    BX, word ptr tcath
  406.     mov    AH,WrtStrm        ;write 1 char to -catalog file stream
  407.     int    dos
  408.     jnc    putcat_4
  409.     cmp    AX,5
  410.     jz    putcat_1
  411.     cmp    AX,6
  412.     jz    putcat_2
  413.     ret
  414. PutCat_1:
  415.     mov    DX,offset FullMsg
  416.     jmp    short putcat_3
  417. PutCat_2:
  418.     mov    DX,offset dtamsg
  419. PutCat_3:
  420.     mov    AH,print
  421.     int    dos
  422.     jmp    FilErr
  423. PutCat_4:
  424.     ret
  425.  
  426. MakeCat:
  427.     mov    DX, offset tcatz    ;z-string of tmp catalog name
  428.     xor    CX,CX            ;no file attribute
  429.     mov    AH,CrtStrm        ;create -catalog file stream
  430.     int    dos
  431.     mov    word ptr tcath,AX
  432.     jnc    makecat_1
  433.     mov    AH,print
  434.     mov    DX,offset space
  435.     int    dos
  436.     jmp    FilErr
  437.  
  438. makecat_1:
  439.         if gen_header
  440.     ;*******
  441.     mov    SI,offset fname1    ;plug catalog number into header
  442.     mov    DI,offset header1
  443.     mov    CX,3
  444.     rep    movsb
  445.     mov    BX,offset header    ;print the header
  446.     call    display
  447.     ;*******
  448.         endif
  449.  
  450. again:
  451.     call    MFname            ;SEARCH FOR NAMES
  452.     jnae    agn_2    
  453.     jmp    NameTest        ;ANOTHER FOUND, print NAME
  454.  
  455. agn_2:
  456.     mov    AL,byte ptr MFflg    ;NOTHING FOUND, CHECK
  457.     or    AL,AL            ;  NAME FOUND FLAG
  458.     jz    done            ;AT LEAST ONE WAS FOUND
  459.     jmp    abext            ;print msg, then exit
  460.  
  461.     ; close -CATALOG.$$$
  462.     ;
  463. done:                    ;close -CATALOG.$$$
  464.         if gen_footer
  465.     ;*******
  466.     mov    SI,offset fname1    ;plug catalog number into footer
  467.     mov    DI,offset DoneExt
  468.     mov    CX,3
  469.     rep    movsb
  470.     mov    BX,offset foot1        ;print the footer
  471.     call    display
  472.  
  473.     mov    BL,byte ptr linenum    ; binary line number into BX
  474.     xor    BH,BH
  475.     mov    AL,' '            ; fill character is ascii space
  476.     mov    CX,2            ; decimal digit limit
  477.     mov    DI,offset foot2        ; pointer to ascii string buffer
  478.     call    bin_2_dec        ; binary to decimal & plug buffer
  479.  
  480.     mov    BX,offset foot2
  481.     call    display
  482.  
  483.         ;***
  484.         else
  485.         ;***
  486.  
  487.     call    crlf
  488.     ;*******
  489.         endif
  490.  
  491. done_2:
  492.     mov    AL,eof
  493.     call    PutCatalog
  494.     mov    BX, word ptr tcath
  495.     mov    AH,CloStrm        ;close -catalog file stream
  496.     int    dos
  497.     cmp    AX,6
  498.     jnz    done_3
  499.     mov    AH,print
  500.     mov    DX,offset ClsMsg
  501.     int    dos
  502. done_3:
  503.     mov    byte ptr Wflag,' '    ;indicate write file closed
  504.     mov    SI, offset fname1    ;point to volume # string
  505.     mov    DI,offset fcbnew+9    ;file type for rename & delete
  506.     mov    CX,3            ;byte count
  507.     rep    movsb            ;block move
  508.  
  509.     ;    ERASE ANY EXISTING OLD FILE
  510.     ;
  511. erase:
  512.     mov    AH,delete
  513.     mov    DX,offset fcbnew
  514.     int    dos
  515.  
  516.     ;    rename -CATALOG.$$$ to -CATALOG.NNN
  517.     ;
  518. rens:
  519.     mov    DI,offset fcbCat+16
  520.     mov    SI,offset fcbnew
  521.     mov    CX,12
  522.     rep    movsb
  523.     mov    DX, offset fcbCat
  524.     mov    AH,rename
  525.     int    dos
  526.  
  527.     ; exit to dos
  528.     ;
  529. done_5:
  530.     mov    BX,offset DoneMsg    ;Display end then exit 
  531.     call    display
  532.     jmp    exit
  533.  
  534.     ; test the filename &/or filetype for files to ignore
  535.     ; currently set up to conditionally ignore filenames
  536.     ; beginning with a dash (eg., -CATALOG) but to always
  537.     ; ignore .$$$ filetypes which are usually temporary files
  538.     ;
  539. NameTest:
  540.         if not crc_dash
  541.     ;*******
  542.     cmp    byte ptr fcb+1,'-'    ; see if filename starts with a dash
  543.     jnz    nametest_0        ; continue if not
  544.     jmp    again            ; else skip it
  545.  
  546. nametest_0:
  547.     ;*******
  548.         endif
  549.  
  550.     mov    BX,offset fcb+9        ;POINT TO FILETYPE IN fcb
  551.     call    TestBad            ;CHECK FOR .$$$ FILES
  552.     jnz    NameTest_1    
  553.     jmp    again            ;IF ZERO FLAG, IGNORE THEM
  554.  
  555. NameTest_1:
  556.     mov    BX,offset number
  557.     call    display
  558.  
  559.     mov    AL,'0'            ; fill character is ascii zero
  560.     mov    BL,byte ptr linenum    ; put binary line number into BX
  561.     inc    BL            ; increment it
  562.     mov    byte ptr linenum,BL    ; put it back
  563.     xor    BH,BH
  564.     mov    CX,2            ; decimal digit limit
  565.     mov    DI,offset filen        ; pointer to ascii string buffer
  566.     call    bin_2_dec        ; binary to decimal & plug buffer
  567.  
  568.  
  569.     ;MOVE 8 CHARACTERS FROM fcb+1 TO fname
  570.     ;
  571.     mov    SI,offset fcb+1
  572.     mov    DI,offset fname
  573.     mov    CX,8
  574.     rep    movsb
  575.  
  576.     ;MOVE 3 CHARACTERS FROM fcb+9 TO fname+9
  577.     ;
  578.     inc    DI
  579.     mov    CX,3
  580.     rep    movsb
  581.  
  582.     ;now print the line number & the filename
  583.     ;
  584.     mov    BX,offset filen
  585.     call    display
  586. FNAME2:
  587.     call    opn            ;open FILE AND BUILD CRC
  588.     jnz    fname_3    
  589.     jmp    again            ;BRANCH IF BAD open or read
  590.  
  591. fname_3:
  592.     jmp    abext            ;OTHERWISE GET OUT
  593.  
  594.     ;open THE FILE
  595.     ;
  596. opn:
  597.     mov    DX,offset fcb
  598.     mov    AH,open
  599.     int    dos
  600.     inc    AL
  601.     jnz    RdInit
  602.     mov    BX,offset fail
  603.     call    display
  604.     jmp    BADRET            ;INDICATE BAD open
  605.  
  606.     ;initialize registers used to store variables
  607.     ;
  608. rdinit:
  609.     xor    BX,BX            ; init CRC accumulator to 0
  610.     mov    word ptr quanek,BX    ; init 128 byte block counter
  611.     mov    BP,0A097h        ; init CCIT CRC polynomial mask word
  612.  
  613.     ; this is the buffer refilling routine
  614.     ;
  615. filldta:
  616.     mov    SI,offset dta        ; point to begining of buffer
  617.     mov    CX,sect            ; logical sector size
  618.     mov    DX,offset fcb        ; file control block address
  619.     mov    AH,read            ; read sequential
  620.     int    dos
  621.     or    AL,AL            ; see if full sector returned
  622.     jz    filldta_1        ; if so, go to it
  623.     cmp    AL,3            ; see if partial sector returned
  624.     jnz    finish            ; if not, then eof or read error
  625. filldta_1:
  626.     mov    DL,80h            ; setup q-bit mask
  627.     inc    word ptr quanek        ;increment the block counter
  628.  
  629.  
  630.     ; crc routine based on article by Fred Gutman
  631.     ; in 'EDN' magazine June 5, 1979 page 84
  632.     ; optimized for the 8086 by Howard Vigorita
  633.     ;---------------------------------------------
  634.     ;
  635. crcit:
  636.     test    BH,DL            ; q-bit mask
  637.     lahf                ; save status flags
  638.     shl    BX,1            ; double old crc
  639.     lodsb                ; AL = byte ptr [SI++]
  640.     add    BL,AL            ; add in new byte
  641.     sahf                ; restore the flags
  642.     jz    crcit_1            ; no mask if q-bit was zero
  643.     xor    BX,BP            ; CRC-16 polynomial mask
  644. crcit_1:
  645.     loop    crcit            ; crc next character
  646.     jmp    short filldta        ; if none left, get some more
  647.  
  648. finish:
  649.     mov    word ptr remain,BX    ;save the accumulated CRC remainder
  650.     cmp    AL,1            ;physical end of file?
  651.     jnz    FilErr            ;no, then read error
  652.  
  653.     call    closer
  654.     mov    BX,word ptr quanek    ; count of 128 byte blocks
  655.     add    BX,7            ; bump to next 8 block boundary
  656.     shr    BX,1            ; turn into 1K block count
  657.     shr    BX,1            ; ..  by dividing by 2
  658.     shr    BX,1            ;   ..  three times
  659.  
  660.     mov    AL,' '            ; fill character is ascii space
  661.     mov    CX,3            ; decimal digit limit
  662.     mov    DI,offset ksize        ; pointer to ascii string buffer
  663.     call    bin_2_dec        ; binary to decimal & plug buffer
  664.  
  665.     mov    BX,offset ksize        ; display size in K
  666.     call    display
  667.     mov    AL,byte ptr remain+1    ;GET MSP OF CRC
  668.     call    HEXO            ;print IT
  669.     mov    AL,' '
  670.     call    typer            ;TYPE A space
  671.     mov    AL,byte ptr remain    ;GET LSP OF CRC
  672.     call    HEXO            ;print IT
  673.     call    CLOSER
  674. GOODRET:
  675.     xor    AL,AL
  676.     ret                ;RETURN TO CALLER WITH ZERO FLAG SET
  677.  
  678. FilErr:
  679.     mov    BX,offset ferror
  680.     call    display            ;print THE MESSAGE AND RETURN
  681. BADRET:                    ;GENERAL BAD RETURN LOCATION
  682.     xor    AL,AL            ;SET ZERO FLAG
  683.     inc    AL            ;RESET ZERO FLAG
  684.     ret                ;RETURN TO CALLER
  685.  
  686. CLOSER:
  687.     mov    DX,offset fcb
  688.     mov    AH,close
  689.     int    dos
  690.     ret
  691.  
  692.     ;HEX OUTPUT
  693.     ;
  694. HEXO:
  695.     lahf                ;SAVE FOR RIGHT DIGIT
  696.     xchg    AL,AH
  697.     push    AX
  698.     xchg    AL,AH
  699.     RCR    AL,1            ;RIGHT..
  700.     RCR    AL,1            ;..JUSTIFY..
  701.     RCR    AL,1            ;..LEFT..
  702.     RCR    AL,1            ;..DIGIT..
  703.     call    NIBBL            ;print LEFT DIGIT
  704.     pop    AX            ;RESTORE RIGHT
  705.     xchg    AL,AH
  706.  
  707. NIBBL:
  708.     and    AL,0FH            ;ISOLATE DIGIT
  709.     cmp    AL,10            ;IS IS <10?
  710.     jb    ISNUM            ;YES, NOT ALPHA
  711.     add    AL,7            ;add ALPHA BIAS
  712.  
  713. ISNUM:
  714.     add    AL,'0'            ;make PRINTABLE
  715.     jmp    short typer        ;print IT, THEN RETURN
  716.  
  717.     ; console string outputer via AL
  718.     ; expects string pointer in BX, strings terminated with $
  719.     ;
  720. display:
  721.     mov    AL,byte ptr [BX]    ;GET CHAR
  722.     cmp    AL,'$'
  723.     jz    display_2
  724. display_1:
  725.     call    typer            ;OUTPUT IT
  726.     inc    BX            ;POINT TO NEXT
  727.     mov    AL,byte ptr [BX]    ;TEST
  728.     cmp    AL,'$'            ;..FOR END
  729.     jnz    display_1
  730. display_2:
  731.     ret                ;ret PAST MSG
  732.  
  733.     ; output new line via AL
  734.     ;
  735. CrLf:
  736.     mov    AL,cr            ;CARRIAGE RETURN
  737.     call    typer
  738.     mov    AL,lf            ;LINE FEED, FALL INTO 'TYPE'
  739.  
  740.     ; send character in AL to console & output file
  741.     ;
  742. typer:
  743.     push    BX
  744.     and    AL,7FH            ;STRIP PARITY BIT
  745.     mov    DL,AL
  746.     push    DX
  747.     call    WRFILE            ;write TO FILE IF REQUESTED
  748.     pop    DX
  749.     mov    byte ptr catbuf,DL
  750.     mov    DX, offset catbuf
  751.     mov    CX,1
  752.     mov    AH,WrtStrm        ;write 1 char to output stream
  753.     int    dos
  754.     pop    BX
  755.     ret
  756.  
  757.     ;    write character in DL register to output file
  758.     ;    if the file switch is set
  759.     ;
  760. WRFILE:
  761.     mov    AL,byte ptr Wflag    ;get write file flag
  762.     cmp    AL,'F'            ;see if it's set
  763.     jz    wrf_1            ;go to file write if so
  764.     mov    BX,2            ;else, set up error output stream
  765.     ret                ;and return
  766.  
  767. wrf_1:
  768.     mov    AL,DL            ;get character back
  769.     call    PutCatalog
  770.     mov    BX,1            ;setup redirection stream
  771.     ret                ;and return
  772.  
  773.     ;MULTI-FILE ACCESS SUBROUTINE. ALLOWS PROCESSING
  774.     ;OF MULTIPLE FILES <I.E. *.ASM> FROM DISK.  THIS
  775.     ;ROUTINE BUILDS THE PROPER NAME IN THE fcb EACH
  776.     ;TIME IT IS CALLED.  CARRY IS SET IF NO MORE NAMES
  777.     ;CAN BE FOUND.  THE ROUTINE IS COMMENTED IN PSEUDO
  778.     ;CODE, EACH PSEUDO CODE STATEMENT IS IN <<...>>
  779.     ;
  780. MFname:
  781.     mov    AH,stdma
  782.     mov    DX, offset dta
  783.     int    dos
  784.     xor    AL,AL
  785.     mov    byte ptr FCBEXT,AL
  786.     mov    byte ptr FCBRNO,AL
  787.  
  788.     ;<<IF FIRST TIME>>
  789.     mov    AL,byte ptr MFflg
  790.     or    AL,AL
  791.     jz    MFN01
  792.  
  793.     ;<<SAVE THE REQUESTED NAME>>
  794.     ;SAVE ORIG REQUEST
  795.     mov    SI,offset fcb
  796.     mov    DI,offset mfreq
  797.     mov    CX,12
  798.     rep    movsb
  799.     mov    AL,byte ptr fcb
  800.     mov    byte ptr mfcur,AL    ;SAVE DISK IN CURR fcb
  801.  
  802.     ;<<srchf REQUESTED NAMD>>
  803.     rep    movsb
  804.     mov    AH,srchf
  805.     mov    DX,offset fcb
  806.     int    dos
  807.  
  808.     ;<<ELSE>>
  809.     jmp    short MFN02
  810.  
  811. MFN01:                    ;<<srchf CURRENT NAME>>
  812.     mov    SI,offset mfcur
  813.     mov    DI,offset fcb
  814.     mov    CX,12
  815.     rep    movsb
  816.     mov    AH,srchf
  817.     mov    DX,offset fcb
  818.     int    dos
  819.  
  820.     ;<<srchn REQUESTED NAME>>
  821.     mov    SI,offset mfreq
  822.     mov    DI,offset fcb
  823.     mov    CX,12
  824.     rep    movsb
  825.     mov    AH,srchn
  826.     mov    DX,offset fcb
  827.     int    dos
  828.  
  829.     ;<<ENDIF>>
  830. MFN02:                    ;<<RETURN CARRY IF NOT FOUND>>
  831.     inc    AL
  832.     stc
  833.     jnz    MFN03
  834.     ret
  835.  
  836.     ;<<MOVE NAME FOUND TO CURRENT NAME>>
  837. MFN03:
  838.     mov    SI, offset dta+1
  839.     push    SI            ;SAVE NAME POINTER
  840.     mov    DI,offset mfcur+1
  841.     mov    CX,11
  842.     rep    movsb
  843.  
  844.     ;<<MOVE NAME FOUND TO fcb>>
  845.     pop    SI
  846.     mov    DI,offset fcb+1
  847.     mov    CX,11
  848.     rep    movsb
  849.  
  850.     ;<<SETUP fcb>>
  851.     xor    AL,AL
  852.     mov    byte ptr FCBEXT,AL
  853.     mov    byte ptr FCBRNO,AL
  854.     mov    byte ptr MFflg,AL    ;TURN OFF 1ST TIME SW
  855.  
  856.     ;<<RETURN>>
  857.     ret
  858.  
  859.     ;CHECK FOR .$$$ FILES
  860.     ;
  861. TestBad:
  862.     call    TESTIT            ;CHECK FIRST ONE FOR '$'
  863.     jz    TestBad_1    
  864.     ret                ;NO, RETURN
  865.  
  866. TestBad_1:
  867.     call    TESTIT            ;CHECK SECOND ONE
  868.     jz    TESTIT    
  869.     ret                ;NO, RETURN
  870.  
  871.     ;FALL INTO TESTIT TO CHECK THIRD
  872.     ;
  873. TESTIT:
  874.     mov    AL,byte ptr [BX]
  875.     and    AL,7FH            ;STRIP ATTRIBUTE
  876.     cmp    AL,'$'            ;CHECK FOR $ FILETYPE
  877.     lahf
  878.     inc    BX
  879.     sahf
  880.     ret
  881.  
  882.  
  883.     ; ABORTED - tell operator and close any inconplete file
  884.     ; to update the FAT, then erase it
  885.     ;
  886. abext:
  887.     mov    AL,byte ptr Wflag    ;SEE IF WE ARE MAKING FILE
  888.     cmp    AL,'F'
  889.     jnz    abext_3            ;NO FILE, SKIP FILE STUFF
  890. abext_1:
  891.     mov    AL,eof
  892.     call    PutCatalog
  893.     mov    BX, word ptr tcath
  894.     mov    AH,CloStrm        ;close -catalog file stream
  895.     int    dos
  896.     jnc    abext_2
  897.     mov    AH,print
  898.     mov    DX,offset ClsMsg
  899.     int    dos
  900. abext_2:
  901.     mov    AH,delete
  902.     mov    DX,offset fcbCat
  903.     int    dos
  904.  
  905. abext_3:
  906.     mov    DX,offset abort
  907.     mov    AH,print
  908.     int    dos
  909.  
  910.     ; terminate this program
  911.     ;
  912. exit:
  913.     mov    AH,TrmProc        ; terminate process
  914.     int    dos
  915.  
  916.  
  917.     ; convert binary word to ascii decimal & write it in buffer
  918.     ; uses the 8086 divide instruction
  919.     ; parameters passed in registres as follows:
  920.     ;    AL    leading 0 fill character (usually 0 or space)
  921.     ;    BX    binary word to be converted
  922.     ;    CX    decimal digit limit (NTE 5)
  923.     ;    DX    (not preserved)
  924.     ;    SI    (not preserved)
  925.     ;    DI    address of buffer to put ascii decimals into
  926. bin_2_dec:
  927.  
  928.     rep    stosb            ; block fill, DI += CX
  929.     mov    AX,BX            ; put binary word into AX
  930.     mov    SI,10            ; put divisor in SI
  931.  
  932. next_digit:
  933.  
  934.     xor    DX,DX            ; clear dividend high word
  935.     div    SI            ; AX = (DX:AX)/SI, DX = remainder
  936.     add    DX,'0'            ; convert DL remainder byte to ascii
  937.     dec    DI            ; back step in buffer
  938.     mov    byte ptr [DI], DL    ; put character there
  939.     or    AX,AX            ; all done? (AX = 0?)
  940.     jnz    next_digit        ; if not, do another digit
  941.     ret
  942.  
  943.  
  944. code    ENDS            ; end of code segment
  945.  
  946.  
  947.  
  948. ; STACK LOCATION
  949. ; --------------
  950. ;
  951. a_stack    SEGMENT PARA STACK
  952.  
  953.     ; use the default disk transfer area & default fcb as a 165 byte stack
  954.  
  955. a_stack    ENDS
  956.  
  957.  
  958.     END    crcbuild
  959.